module Trifle
  module Ruby
    module Instrumentation
      class Moped
        def self.get_collection_with_operation( operations )
          operation = operations.first
          log_statement = operation.log_inspect.encode( "UTF-8" )
          collection = "Unknown"
          if operation.respond_to?( :collection )
            collection = operation.collection
          end
          operation_name = log_statement.split[ 0 ]
          if operation_name == "COMMAND" && log_statement.include?( ":mapreduce" )
            operation_name = "MAPREDUCE"
            collection = log_statement[ /:mapreduce=>"([^"]+)/,1 ]
          elsif operation_name == "COMMAND" && log_statement.include?( ":count" )
            operation_name = "COUNT"
            collection = log_statement[ /:count=>"([^"]+)/,1 ]
          elsif operation_name == "COMMAND" && log_statement.include?( ":aggregate" )
            operation_name = "AGGREGATE"
            collection = log_statement[ /:aggregate=>"([^"]+)/,1 ]
          elsif operation_name == "COMMAND" && log_statement.include?( ":findAndModify" )
            operation_name = "FIND_AND_MODIFY"
            collection = log_statement[ /:findAndModify=>"([^"]+)/,1 ]
          end
          "#{collection} #{operation_name.capitalize}"
        end
      end
    end
  end
end
